#!/bin/sh

# Slackware build script for clearsilver.
#
# Written by Lyle Sigurdson <lyle@sent.com>.
#
# Free for any use; no warranty.

# Exit immediately if a simple command exits with a non-zero status.
set -e

PRGNAM=clearsilver
VERSION=0.10.5
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
tar -xzvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $TMP/$PRGNAM-$VERSION
chown -R root:root .
chmod -R u+rw,go-w,a-s .

CFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --disable-apache \
  --disable-java \
  --disable-ruby \
  --disable-csharp \
  --with-python=/usr/bin/python

make
make INSTALLDIRS=vendor install DESTDIR=$PKG

# Let's build the libneo shared object anyway, even though we've disabled C#
# This is useful to have around for some other applications
cd dso
  make
  make install DESTDIR=$PKG
cd -
 
# Put a link to the libneo shared object in /usr/lib
( cd $PKG/usr/lib ; ln -s python2.5/site-packages/libneo.so libneo.so )

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mv $PKG/usr/share/man/man3/* $PKG/usr/man/man3/
rm -rf $PKG/usr/share

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

# Remove perllocal.pod and other special files that don't need to be installed,
# because they will overwrite what's already on the system.
( cd $PKG
  find . -name perllocal.pod \
    -o -name ".packlist" \
    -o -name "*.bs" \
    | xargs rm -f
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CS_LICENSE INSTALL LICENSE README README.python \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/clearsilver.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/clearsilver.SlackBuild

mkdir -p $PKG/install
cp $CWD/slack-desc $PKG/install

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
